package com.example.ntpsynchronoustime;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;

import com.example.ntpsynchronoustime.utils.DateUtil;
import com.example.ntpsynchronoustime.utils.NetworkTimeUtil;
import com.example.ntpsynchronoustime.utils.NetworkUtil;
import com.example.ntpsynchronoustime.utils.Singleton;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        changeTime();
    }

    private void changeTime() {
        long time = System.currentTimeMillis();//获取本地时间
        if (time < DateUtil.getActiveTimelong("2023-07-02 23:51:34")) {//如果时间小于我们的打包时间
            //通过单例模板获取对象
            Singleton<NetworkTimeUtil> networkTimeSingleton = new Singleton<NetworkTimeUtil>() {
                @Override
                protected NetworkTimeUtil create() {
                    return new NetworkTimeUtil();
                }
            };
            if (networkTimeSingleton.get() != null) {
                long networkTime = networkTimeSingleton.get().getNetworkTime();
                //获取到网络时间
                if (networkTime > 0) {
                    SystemClock.setCurrentTimeMillis(networkTime); // 设置系统时间
                } else if (networkTime == 0) {//如果所有地址都没请求成功
                    SystemClock.setCurrentTimeMillis(DateUtil.getActiveTimelong("2023-07-02 23:51:34")); // 设置打包时间，不至于差别太大
                    initData();
                }
            }
            try {

            } catch (Exception e) {
                Log.i(TAG, "----------------->" + e.toString());
            }
            new Thread(() -> {

            }).start();
        }
    }

    private void initData() {
       new Handler().post(new Runnable() {
           @Override
           public void run() {
               findViewById(R.id.tv_Test).setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       for(int i=0;i<=3000;i++){
                           NetworkUtil.getInstance().pingIp1();
                       }
                   }
               });

           }
       });

    }
}